package com.steven.concurrency.example.sync;

import com.steven.concurrency.annoations.ThreadSafe;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@ThreadSafe
public class Synchronized2 {



    public  void test1(){
        /**
         *  所有线程中只允许有一个线程执行此方法
         */
        synchronized (Synchronized2.class){
            for (int i = 0; i <10 ; i++) {
                System.out.println(i);
            }
        }
    }

    /**
     * 所有线程中只允许有一个线程执行此方法
     */
    public static synchronized void test2(){
        for (int i = 0; i <10 ; i++) {
            System.out.println(i);
        }
    }

    public static void main(String[] args) {
        Synchronized2 synchronized1 = new Synchronized2();
        Synchronized2 synchronized2 = new Synchronized2();
        ExecutorService executorService = Executors.newCachedThreadPool();

        executorService.execute(()-> synchronized1.test1());
        executorService.execute(()-> synchronized2.test1());
    }
}
